<template>
  <el-dialog
  title="新增目录"
  :visible="dialogVisible"
  width="30%"
  @close="handleClose">
  <el-form ref="form" :rules="rules" :model="form" label-width="80px">
      <el-form-item label="所属学科" prop="subjectID">
        <el-select style="width:100%" v-model="form.subjectID" placeholder="请选择">
          <el-option v-for="itm in selectData" :key="itm.value" :label="itm.label" :value="itm.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="目录名称" prop="directoryName">
        <el-input v-model="form.directoryName" placeholder="请输入目录名称"></el-input>
      </el-form-item>
  </el-form>
  <div style="text-align: right;margin-top: 40px;">
    <el-button @click="handleClose">取 消</el-button>
    <el-button type="primary" @click="hGetClose">确 定</el-button>
  </div>
</el-dialog>
</template>

<script>
import { add, update } from '@/api/hmmm/directorys'
export default {
  data () {
    return {
      form: {
        directoryName: '',
        subjectID: null
      },
      rules: {
        directoryName: [{ required: true, message: '请选择目录', trigger: 'blur' }],
        subjectID: [{ required: true, message: '请输入目录名称', trigger: 'change' }]
      }
    }
  },
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    selectData: {
      type: Array
    }
  },
  methods: {
    handleClose () {
      this.$emit('handle-close')
      this.$refs.form.resetFields()
    },
    hGetClose () {
      this.$refs.form.validate(async vali => {
        if (vali) {
          if (this.form.id) {
            await update(this.form)
          } else {
            await add(this.form)
          }
          this.$message.success(this.form.id ? '修改成功' : '新增成功!')
          this.handleClose()
        }
      })
    }
  }
}
</script>

<style scoped lang='less'></style>
